export default {
  // 点击加入购物车
  addToCart ({ cart }, { detail }) {
    const isExit = cart.some(shop => shop.id === detail.id)
    if (isExit) {
      cart = cart.map(shop => {
        if (shop.id === detail.id) shop.count++
        return shop
      })
    } else {
      cart.push({
        ...detail,
        count: 1,
        check: true
      })
    }
  },
  // 点击单选框
  toggleCheck ({ cart }, { id }) {
    cart = cart.map(shop => {
      if (shop.id === id) shop.check = !shop.check
      return shop
    })
  },
  // 点击全选框
  toggleAllCheck ({ cart }, { isAllCheck }) {
    cart = cart.map(shop => {
      shop.check = !isAllCheck
    })
  },
  // // 点击添加按钮
  // addCount ({ cart }, { id }) {
  //   cart = cart.map(shop => {
  //     if (shop.id === id) shop.count++
  //     return shop
  //   })
  // },
  // // 点击减少按钮
  // subCount ({ cart }, { id }) {
  //   cart = cart.map(shop => {
  //     if (shop.id === id) shop.count--
  //     return shop
  //   })
  // },
  // 改变数量
  changeNum (state, { id, num }) {
    // console.log({ id, num })
    state.cart = state.cart.map(shop => {
      if (shop.id === id) shop.count = num
      return shop
    })
  },
  // 删除按钮
  delItem (state, { id }) {
    state.cart = state.cart.filter(shop => shop.id !== id)
  },
  //  改变title 和头部显示
  isBackShowAndChangeTitle (state, { title, isBackShow }) {
    state.title = title
    state.isBackShow = isBackShow
  }
}
